% computes predicted coherence
% between three channels
% input data in EDI spectra pack
% latest date 21.03.2003
% Reference : (1) Bendat, J.S. and Piersol, A.G. 1971. Random data: Analysis and measurement procedures\
% John Wiley & Sons, NY. Pages 153-169; (2)  Reference : Ellinghaus, A. 1997. ProcMT User's manual. Metronix GmBH, Braunschweig, Germany
% tested with Geotools - correct 24.03.2003




function[c] = pcoh(SPM,flag);

if isstruct(SPM) == 1,
   for i = 1:length(SPM.spectra),
      SPM1(i,:,:)=SPM.spectra(i).data;
   end;
   clear SPM;
   SPM=SPM1;
end;


A = size(SPM);
k = length(A);
j = sqrt(-1);
if flag == 'Ex',
   x = 3;
   y = 1;
elseif flag == 'Ey',
   x = 2;
   y = 4;
else,
   fprintf('pcoh-> Allowed flags Ex  Ey\n');
   c=999;
   return;
end;


if k == 2,
   data(:,:)=SPM;
   t = tf_dn(data);
   c = cohe(t(x),t(y),data,flag);
   
elseif k == 3,
   t = tf_dn(SPM);
   for i = 1:A(1),
      data(:,:) = SPM(i,:,:);
      c(i) = cohe(t(i,x),t(i,y),data,flag);
   end;
elseif k==4,
   for ii = 1:A(2),
      SPMii(:,:,:) = SPM(:,ii,:,:);
      t = tf_dn(SPMii);
      for i = 1:A(1),
         data(:,:) = SPM(i,ii,:,:);
         c(i,ii) = cohe(t(i,x),t(i,y),data,flag);
      end;
   end;
 end;


         
      

